load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
load("//rs/tests:common.bzl", "MAINNET_ENV", "MESSAGE_CANISTER_ENV", "MESSAGE_CANISTER_RUNTIME_DEPS", "UNIVERSAL_VM_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "system_test_nns")

package(default_visibility = ["//rs:system-tests-pkg"])

rust_library(
    name = "common",
    testonly = True,
    srcs = ["common.rs"],
    crate_name = "ic_consensus_system_test_upgrade_common",
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:futures",
        "@crate_index//:ic-agent",
        "@crate_index//:ic-utils",
        "@crate_index//:regex",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "upgrade_downgrade_app_subnet_test",
    colocated_test_driver_vm_resources = {
        "vcpus": 24,
    },
    env = MAINNET_ENV | MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",
    ],
    test_timeout = "eternal",
    uses_guestos_img = False,
    uses_guestos_mainnet_nns_img = True,
    uses_guestos_update = True,
    runtime_deps =
        MESSAGE_CANISTER_RUNTIME_DEPS +
        UNIVERSAL_VM_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":common",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:futures",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "upgrade_downgrade_nns_subnet_test",
    env = MAINNET_ENV | MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",
    ],
    test_timeout = "eternal",
    uses_guestos_img = False,
    uses_guestos_mainnet_nns_img = True,
    uses_guestos_update = True,
    runtime_deps =
        MESSAGE_CANISTER_RUNTIME_DEPS +
        UNIVERSAL_VM_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":common",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
    ],
)

rust_binary(
    name = "upgrade_test_bin",
    testonly = True,
    srcs = ["upgrade_app_subnet_test.rs"],
    crate_name = "ic_systest_upgrade_app_subnet",
    deps = [
        # Keep sorted.
        ":common",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "upgrade_app_subnet_test",
    env = MAINNET_ENV | MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 2.27% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    test_driver_target = ":upgrade_test_bin",
    test_timeout = "eternal",  # the default 900 seconds (15 minutes) is not enough for this test, so we set it to 3600 seconds (1 hour).
    uses_guestos_img = False,
    uses_guestos_mainnet_nns_img = True,
    uses_guestos_update = True,
    runtime_deps =
        MESSAGE_CANISTER_RUNTIME_DEPS +
        UNIVERSAL_VM_RUNTIME_DEPS,
)

system_test_nns(
    name = "downgrade_app_subnet_test",
    env = MAINNET_ENV | MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 2.97% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    test_driver_target = ":upgrade_test_bin",
    test_timeout = "eternal",  # the default 900 seconds (15 minutes) is not enough for this test, so we set it to 3600 seconds (1 hour).
    uses_guestos_img = True,
    uses_guestos_mainnet_nns_update = True,
    runtime_deps =
        MESSAGE_CANISTER_RUNTIME_DEPS +
        UNIVERSAL_VM_RUNTIME_DEPS,
)

system_test_nns(
    name = "upgrade_with_alternative_urls",
    tags = [
        "long_test",
    ],
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = UNIVERSAL_VM_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/common",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
    ],
)

system_test_nns(
    name = "unassigned_node_upgrade_test",
    tags = [
        "long_test",
    ],
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = UNIVERSAL_VM_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/canister_client",
        "//rs/nervous_system/common/test_keys",
        "//rs/nns/common",
        "//rs/registry/nns_data_provider",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
    ],
)
